**Author: Abeberese, Barnwal, Chaurey, Mukherjee
**This do-file will generate Figure 3 and Tables 2, A5, A6, A7, A8, A9
**Date: May 2021

clear
capture log close
set matsize 4000
set scheme s2mono, perm


**Set directories
*Add personal root path for your machine directory

global ROOT "/Users/rchaurey/Dropbox/Indonesia Firms/DataFolderRESTAT/"
global dofiles "${ROOT}/Code"
global data "${ROOT}/Input"
global output "${ROOT}/Output/"



********************************************************************************
*Figure 3, Table A5 and Table A6
********************************************************************************
use "${data}/regulations", clear
gen t = year - mayor_appyr -5

replace t=4 if t>=4 & t<.

xi i.t, noomit
*des _It_bin*
des _It_*
gen zero=0

label var _It_1 "[-4]"
label var _It_2 "[-3]"
label var _It_3 "[-2]"

label var zero "[-1]"
label var _It_5 "[0]"
label var _It_6 "[1]"
label var _It_7 "[2]"
label var _It_8 "[3]"
label var _It_9 "[4]"
reghdfe overall _It_1-_It_3 zero _It_5-_It_9 , absorb(district year) cluster(district)
estimates store modelregulation

outreg2 using "${output}regulation_Full.xls", ///
keep(_It* zero)   addtext(district FE, Yes, year FE, Yes) replace

coefplot modelregulation, vertical ciopts(recast(rcap) lcolor(gs8) lpattern(solid) ) color(black)  recast(connected) keep(_It* zero) omitted levels (90) title ("regulation quality") graphregion(color(white))  ///
xtitle("Years since democratization") bgcolor(white) yline(0, lcolor(gs14) lpattern(solid)) xlabel(, labsize(small))  xline(4, lcolor(gs12))
graph save "${output}regulation_Full", replace
graph export "${output}regulation_Full.png", as(png)  replace


reghdfe overall _It_2-_It_3 zero _It_5-_It_9 , absorb(district year) cluster(district)
test _It_2=_It_3=0

reghdfe overall zero _It_5-_It_9 , absorb(district year) cluster(district)
estimates store modelLR
outreg2 using "${output}regulation_Semi.xls", ///
keep(_It* zero)   addtext(district FE, Yes, year FE, Yes) replace

coefplot modelLR, vertical ciopts(recast(rcap)  lcolor(gs8) lpattern(solid)) color(black)  recast(connected) keep(_It* zero) omitted levels (90) title ("regulation quality") graphregion(color(white))  ///
xtitle("Years since democratization") bgcolor(white) yline(0, lcolor(gs14) lpattern(solid)) xlabel(, labsize(small)) 
graph save "${output}regulation_Semi", replace
graph export "${output}regulation_Semi.png", as(png)  replace




****************************************************************************************************
*Table 2 and Table A7
****************************************************************************************************
****************************************************************************************************
*District-level regulation regressions for total number of laws and number of laws in each category
****************************************************************************************************
use "${data}/regulations_districtlevel", clear

gen firstdemyr = mayor_appyr+5
gen post = year>=firstdemyr

reghdfe count_law post, absorb(district year) cluster(district)
outreg2 using "${output}regulation_countDistrictDID.xls", ///
keep(post)   addtext(district FE, Yes, year FE, Yes) replace


reghdfe count_business post, absorb(district year) cluster(district)
outreg2 using "${output}regulation_categoryDistrictDID.xls", ///
keep(post)   addtext(district FE, Yes, year FE, Yes) replace


foreach i in transport labor tax other{
reghdfe count_`i' post, absorb(district year) cluster(district)
outreg2 using "${output}regulation_categoryDistrictDID.xls", ///
keep(post)   addtext(district FE, Yes, year FE, Yes) append
}



****************************************************************************************************
*Table A8 and Table A9
****************************************************************************************************
**************************************************************************************************
*Heterogeneity in effects on productivity and output by quality and number of laws in each category
**************************************************************************************************
**Merging firm data with regulation data
use "${data}/mayor_manufactureBaselineRegressions_longrun", clear
merge m:1 district year using "${data}/regulations_districtlevel", generate(merge_regulations) keepusing(*count*)
keep if merge_regulations==3

gen firstdemyr = mayor_appyr+5
gen post = year>=firstdemyr
replace post = . if mayor_appyr==.

*Interactions with change in number of laws that satisfy all of the KPPOD criteria
gen postchangecount_quality = post*changecount_quality

*Interactions with change in total number of laws
gen postchangecount_law = post*changecount_law

*Interactions with change in number of laws in each category
foreach i in business transport labor tax other{
gen postchangecount_`i'  = post*changecount_`i'
}


*Regressions
reghdfe log_labor_prod post postchangecount_quality postchangecount_law,   absorb( idno ind5year##c.(changecount_quality changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityProd.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) replace
foreach i in business transport labor tax other{
reghdfe log_labor_prod post postchangecount_`i' postchangecount_law,   absorb( idno ind5year##c.(changecount_`i' changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityProd.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) append
}

reghdfe ltfpLP post postchangecount_quality postchangecount_law,   absorb( idno ind5year##c.(changecount_quality changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityProd.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) replace
foreach i in business transport labor tax other{
reghdfe ltfpLP post postchangecount_`i' postchangecount_law,   absorb( idno ind5year##c.(changecount_`i' changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityProd.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) append
}

reghdfe log_v_prod post postchangecount_quality postchangecount_law,   absorb( idno ind5year##c.(changecount_quality changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityOutput.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) replace
foreach i in business transport labor tax other{
reghdfe log_v_prod post postchangecount_`i' postchangecount_law,   absorb( idno ind5year##c.(changecount_`i' changecount_law))  cluster(district)
outreg2 using "${output}regulation_heterogeneityOutput.xls", ///
keep(post*)   addtext(firm FE, Yes, 5 digit industry X year FE, Yes) append
}
